import {
  getInfo,
  register,
  getVerifyCode,
  getstatisticsData,
  getStatInviterCredit,
  getStatInviterData,
  getStatActivityCreditDate,
  getStatPuchaseCreditDate,

} from 'api/user';

import {
  setDoctorInfo,

} from 'api/order';
import {
  getToken,
  setToken,
  getOpenID,
  setOpenID,
  getUserInfo,
  setUserInfo,
  removeUserInfo,
  getUID,
  setUID,
  setPhone,
  getPhone,
  setCode,
  getCode,
  setOrg,
  getOrg,
  setType,
  getType,
  setTpCode,
  getTpCode,
  getRId,
  setRId,
  getCrc,
  setCrc,
  getHospitalId,
  setHospitalId,
  gethzTestStatus,
  sethzTestStatus
} from 'service/auth';

import { flushCookie } from 'common/utils'

const user = {
  state: {
      openid: getOpenID(),
      token: getToken(),
      phone: getPhone(),
      uid: getUID(),
      code: getCode(),
      tpcode: getTpCode(),
      org: getOrg(),
      type: getType(),
      insiderName:"",
      crc: getCrc(),
      hospitalId: getHospitalId(),
      hzTestStatus: gethzTestStatus(),
      operatorId: getRId(),
      isMember: false
  },

  mutations: {
      SET_OPENID: (state, openid) => {
          state.openid = openid;
      },
      SET_TOKEN: (state, token) => {
          state.token = token;
      },
      SET_UID: (state, uid) => {
          state.uid = uid;
      },
      SET_CODE: (state, code) => {
          state.code = code;
      },
      SET_ISMEMBER: (state, isMember) => {
          state.isMember = isMember;
      },
      SET_PHONE: (state, phone) => {
          state.phone = phone;
      },
      SET_ORG: (state, org) => {
          state.org = org;
      },
      SET_TYPE: (state, type) => {
          state.type = type;
      },
      SET_TPCODE: (state, tpcode) => {
          state.tpcode = tpcode;
      },
      SET_OPERATORID: (state, operatorId) => {
          state.operatorId = operatorId;
      },
      SET_CRC: (state, crc) => {
          state.crc = crc;
      },
      SET_HOSPITALID: (state, id) => {
          state.hospitalId = id;
      },
      SET_hzTestStatus: (state, hzTestStatus) => {
          state.hzTestStatus = hzTestStatus;
      },
  },

  actions: {

      GetInfo({ commit, state }) {
          return new Promise((resolve, reject) => {
              getInfo().then(response => {
                  console.log('reponse ==', response)
                  const data = response.data;
                  commit('SET_UID', data.id);
                  commit('SET_PHONE', data.phone);
                  commit('SET_CODE', data.code);
                  commit('SET_TPCODE', data.tpCode);
                  commit('SET_ORG', data.unitName);
                  commit('SET_TYPE', data.type);
                  commit('SET_CRC', data.crc);
                  commit('SET_HOSPITALID', data.hospitalId);
                  commit('SET_OPERATORID', data.operatorId);
                  commit('SET_hzTestStatus', data.hzTestStatus);
                  for (let key in data) {
                      if ((data[key] === null) || (data[key] === '')) delete data[key];
                  }

                  setUID(data.id);
                  setPhone(data.phone);
                  setCode(data.code);
                  setOrg(data.unitName);
                  setTpCode(data.tpCode);
                  setType(data.type);
                  setCrc(data.crc);
                  setRId(data.operatorId);
                  setHospitalId(data.hospitalId);
                  sethzTestStatus(data.hzTestStatus);



                  // sessionStorage.setItem('user_type', data.type);
                  // setUserInfo(data);
                  resolve(response);
              }).catch(error => {
                  reject(error);
              });
          });
      },
      LogOut({ commit, state }) {
          return new Promise((resolve, reject) => {
              commit('SET_OPENID', '');
              commit('SET_TOKEN', '');
              commit('SET_UID', '');
              commit('SET_CODE', '');
              commit('SET_ORG', '');
              commit('SET_TYPE', '');
              commit('SET_CRC', '');
              commit('SET_TPCODE', '');
              commit('SET_hzTestStatus', '');
              commit('SET_ISMEMBER', false);
              // removeToken();
              // removeOpenID();
              flushCookie();
              resolve();
          });
      },
      // 注册会员
      Register({ commit }, option) {
          return new Promise((resolve, reject) => {
              register(option).then(response => {
                  resolve(response);
              }).catch(error => {
                  reject(error)
              })
          })
      },
      // 接收验证码
      GetVerifyCode({ commit }, option) {
          console.log('---')
          return new Promise((resolve, reject) => {
              getVerifyCode(option).then(response => {
                  resolve(response);
              }).catch(error => {
                  reject(error)
              })
          })
      },
      GetstatisticsData({ commit }) {
          return new Promise((resolve, reject) => {
              getstatisticsData().then(response => {
                  resolve(response);
              }).catch(error => {
                  reject(error);
              })
          })
      },
      // 邀请会员
      GetStatInviterCredit({ commit }) {
          return new Promise((resolve, reject) => {
              getStatInviterCredit().then(response => {
                  resolve(response);
              }).catch(error => {
                  reject(error);
              })
          })
      },
      // 获取用户今日统计数据
      GetStatInviterData({ commit }) {
          return new Promise((resolve, reject) => {
              getStatInviterData().then(response => {
                  resolve(response);
              }).catch(error => {
                  reject(error);
              })
          })
      },
      // 获取活动积分数据统计
      GetStatActivityCreditDate({ commit }) {
          return new Promise((resolve, reject) => {
              getStatActivityCreditDate().then(response => {
                  resolve(response);
              }).catch(error => {
                  reject(error);
              })
          })
      },
      // 获取购买积分数据统计
      GetStatPuchaseCreditDate({ commit }) {
          return new Promise((resolve, reject) => {
              getStatPuchaseCreditDate().then(response => {
                  resolve(response);
              }).catch(error => {
                  reject(error);
              })
          })
      },
      // 设置医生信息
      SetDoctorInfoAction({ commit },data) {
          return new Promise((resolve, reject) => {
              setDoctorInfo(data).then(response => {
                  commit('SET_HOSPITALID', data.hospitalId);
                  setHospitalId(data.hospitalId);
                  resolve(response);
              }).catch(error => {
                  reject(error);
              })
          })
      }
  }
}

export default user